package com.qiantu.phone.widget.btnview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group;

import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.phone.R;

import java.util.List;

public class TotalBtnView extends ConstraintLayout {
    String defaultName = getContext().getString(R.string.custom_name);
    String key1 = defaultName;
    String key2 = defaultName;
    String key3 = defaultName;
    String key4 = defaultName;
    String key5 = defaultName;
    String key6 = defaultName;
    String key7 = defaultName;
    String key8 = defaultName;
    String key9 = defaultName;
    String key10 = defaultName;

    private int mKeyCount = 2;
    private Group btnView10Ly;
    private OneTwoThreeFourSixEightNineButtonView btnView10_9;
    private OneButtonView btnView10_1;
    private Group btnView7Ly;
    private FiveButtonView btnView7_5;
    private OneButtonView btnView7_2;
    private OneButtonView btnView7_1;
    private OneTwoThreeFourSixEightNineButtonView btnView3;
    private OneTwoThreeFourSixEightNineButtonView btnView2;
    private OneButtonView btnView1;

    public TotalBtnView(@NonNull Context context) {
        this(context, null);
    }

    public TotalBtnView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TotalBtnView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TotalBtnView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(getContext()).inflate(R.layout.total_btn_view, this, true);
        initView();
    }

    private void initView() {
        btnView10Ly = (Group) findViewById(R.id.btn_view10_ly);
        btnView10_9 = (OneTwoThreeFourSixEightNineButtonView) findViewById(R.id.btn_view10_9);
        btnView10_1 = (OneButtonView) findViewById(R.id.btn_view10_1);
        btnView7Ly = (Group) findViewById(R.id.btn_view7_ly);
        btnView7_5 = (FiveButtonView) findViewById(R.id.btn_view7_5);
        btnView7_2 = (OneButtonView) findViewById(R.id.btn_view7_2);
        btnView7_1 = (OneButtonView) findViewById(R.id.btn_view7_1);
        btnView3 = (OneTwoThreeFourSixEightNineButtonView) findViewById(R.id.btn_view3);
        btnView2 = (OneTwoThreeFourSixEightNineButtonView) findViewById(R.id.btn_view2);
        btnView1 = (OneButtonView) findViewById(R.id.btn_view1);
        setKeyCount(mKeyCount);
    }

    public void setKeyCount(int keyCount){
        mKeyCount = keyCount;
        if (btnView1 != null){
            btnView1.setVisibility(GONE);
            btnView2.setVisibility(GONE);
            btnView3.setVisibility(GONE);
            btnView7Ly.setVisibility(GONE);
            btnView10Ly.setVisibility(GONE);
            if (keyCount == 1){
                btnView1.setVisibility(VISIBLE);
            }else if (keyCount == 2){
                btnView2.setVisibility(VISIBLE);
            }else if (keyCount == 3){
                btnView3.setVisibility(VISIBLE);
            }else if (keyCount == 7){
                btnView7Ly.setVisibility(VISIBLE);
            }else if (keyCount == 9){
                btnView10Ly.setVisibility(VISIBLE);
            }
        }
    }

    private OnBtnClickListener mOnBtnClickListener;
    private OnBtnLongClickListener mOnBtnLongClickListener;
    public void setOnBtnClickListener(OnBtnClickListener listener) {
        if (listener != null){
            mOnBtnClickListener = listener;
            btnView10_9.setOnBtnClickListener(new OnBtnClickListener() {
                @Override
                public void onClick(int btnID) {
                    mOnBtnClickListener.onClick(btnID + 1);
                }
            });
            btnView7_5.setOnBtnClickListener(new OnBtnClickListener() {
                @Override
                public void onClick(int btnID) {
                    mOnBtnClickListener.onClick(btnID + 2);
                }
            });
            btnView3.setOnBtnClickListener(mOnBtnClickListener);
            btnView2.setOnBtnClickListener(mOnBtnClickListener);
            btnView1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnBtnClickListener.onClick(1);
                }
            });
            btnView7_1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnBtnClickListener.onClick(1);
                }
            });
            btnView7_2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnBtnClickListener.onClick(2);
                }
            });
            btnView10_1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnBtnClickListener.onClick(1);
                }
            });
        }
    }

    public void setOnBtnLongClickListener(OnBtnLongClickListener listener){
        if (listener != null){
            mOnBtnLongClickListener = listener;
            btnView10_9.setOnBtnLongClickListener(new OnBtnLongClickListener() {
                @Override
                public void onLongClick(int btnID) {
                    mOnBtnLongClickListener.onLongClick(btnID + 1);
                }
            });
            /*
            btnView10_1.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnBtnLongClickListener.onLongClick(1);
                    return true;
                }
            });
            */

            /*
            btnView7_5.setOnBtnLongClickListener(new OnBtnLongClickListener() {
                @Override
                public void onLongClick(int btnID) {
                    mOnBtnLongClickListener.onLongClick(btnID + 2);
                }
            });
            btnView7_1.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnBtnLongClickListener.onLongClick(1);
                    return true;
                }
            });
            btnView7_2.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnBtnLongClickListener.onLongClick(2);
                    return true;
                }
            });
            */

            btnView3.setOnBtnLongClickListener(mOnBtnLongClickListener);
            //btnView2.setOnBtnLongClickListener(mOnBtnLongClickListener);


            /*
            btnView1.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnBtnLongClickListener.onLongClick(1);
                    return true;
                }
            });
            */
        }
    }

    private void setText(){
        if (mKeyCount == 1){
            //不能自定义。
        }else if (mKeyCount == 2){
            //不能自定义。
            //btnView2.setText(key1, key2);
        }else if (mKeyCount == 3){
            btnView3.setText(key1, key2, key3);
        }else if (mKeyCount == 7){
            //不能自定义
        }else if (mKeyCount == 9){
            btnView10_9.setText(key2, key3, key4, key5, key6, key7, key8, key9, key10);
        }
    }

    public void setCustomKeyName(String keyID, String name){
        switch (keyID){
            case "1":
                key1 = name;
                break;
            case "2":
                key2 = name;
                break;
            case "3":
                key3 = name;
                break;
            case "4":
                key4 = name;
                break;
            case "5":
                key5 = name;
                break;
            case "6":
                key6 = name;
                break;
            case "7":
                key7 = name;
                break;
            case "8":
                key8 = name;
                break;
            case "9":
                key9 = name;
                break;
            case "10":
                key10 = name;
                break;
        }
        setText();
    }
    public void initCustomKeyInfo(List<DeviceCustomKeyBean> keyInfos) {
        for(DeviceCustomKeyBean keyData : keyInfos){
            switch (keyData.getKeyIndex()){
                case "1":
                    key1 = keyData.getKeyName();
                    break;
                case "2":
                    key2 = keyData.getKeyName();
                    break;
                case "3":
                    key3 = keyData.getKeyName();
                    break;
                case "4":
                    key4 = keyData.getKeyName();
                    break;
                case "5":
                    key5 = keyData.getKeyName();
                    break;
                case "6":
                    key6 = keyData.getKeyName();
                    break;
                case "7":
                    key7 = keyData.getKeyName();
                    break;
                case "8":
                    key8 = keyData.getKeyName();
                    break;
                case "9":
                    key9 = keyData.getKeyName();
                    break;
                case "10":
                    key10 = keyData.getKeyName();
                    break;
            }
        }
        setText();
    }

}
